<?php
/**
 * Created by PhpStorm.
 * User: deng
 * Date: 2015/3/4
 * Time: 15:00
 */

namespace Labs\Controller;

use Common\Controller\RestFulApiController;


class LabsController extends RestFulApiController
{
    public function __construct()
    {
        parent::__construct();
        $this->_model = $this->_db->labs;
        $this->init();
    }

    public function multiple()
    {
        switch ($this->_method) {
            case 'get':
                return $this->index();
                break;
            case 'post':
                return $this->create();
                break;
            case 'put':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'delete':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'head':
                break;
            case 'options':
                return $this->options('Access-Control-Allow-Methods: GET, POST, OPTIONS');
                break;
            default:
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
        }
    }

    public function single()
    {
        $id = $_REQUEST['labId'];
        switch ($this->_method) {
            case 'get':
                return $this->view($id);
                break;
            case 'post':
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
            case 'put':
                return $this->update($id);
                break;
            case 'delete':
                return $this->delete($id);
                break;
            case 'head':
                break;
            case 'options':
                return $this->options('Access-Control-Allow-Methods: GET, OPTIONS, PUT, DELETE');
                break;
            default:
                E(C('CODE.METHOD_NOT_ALLOWED')['msg'], $status = C('CODE.METHOD_NOT_ALLOWED')['code']);
                break;
        }
    }

    public function index()
    {
        $cond = array_merge(['status' => '1'], $this->_cond);
        $fields = $this->_fields;
        $key = array_search('user', $fields);
        if ($key !== FALSE) {
            unset($fields[$key]);
            $fields[] = 'creatorId';
        }
        $conf = [
            'fields' => $fields,
            'cond' => $cond,
            'orderBy' => $this->_order,
            'skip' => $this->_skip,
            'limit' => $this->_limit
        ];
        if ($this->_action == 'count')
            $result['total'] = $this->_model->findBy($conf)['total'];
        else if ($this->_action == 'list')
            $result = $this->_model->findBy($conf)['list'];
        if (in_array('user', $this->_fields) || (empty($this->_fields) && empty($this->_root))) {
            $userModel = $this->_db->users;
            foreach ($result as $key => $value) {
                $userId = $value['creatorId'];
                $result[$key]['user'] = $userModel->find($userId, ['info', 'role']);
            }
        }
        Success(C('CODE.OK')['code'], $result);
    }

    public function view($id)
    {
        $fields = $this->_fields;
        $key = array_search('user', $fields);
        if ($key !== FALSE) {
            unset($fields[$key]);
            $fields[] = 'creatorId';
        }
        $result = $this->_model->find($id, $fields);
        if (in_array('user', $this->_fields) || (empty($this->_fields) && empty($this->_root))) {
            $userModel = $this->_db->users;
            $result['user'] = $userModel->find($result['creatorId'], ['info', 'role']);
        }
        Success(200, $result);
    }

    public function create()
    {
        $this->_data['creatorId'] = $this->_token['userId'];
        $this->_data['status'] = '1';
        parent::create();
    }

    public function checkAuth()
    {
        parent::checkAuth();
        if ($_REQUEST['labId']) {
            $labModel = $this->_db->labs;
            if ($labModel->find($_REQUEST['labId'])['creatorId'] != $this->_token['userId'])
                ClientError('请求权限不足', c('CODE.UNAUTHORIZED')['code']);
        }
    }
}